package com.lbd99.data.lbd.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbd99.data.lbd.service.XContentFileService;
import com.lbd99.data.lbd.vo.XContentFile;
import com.lbd99.data.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("contentFile")
//@Api(tags = "文件管理", value = "XContentFileController")
public class XContentFileController {
    @Autowired
    private XContentFileService xContentFileService;

    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    //@ApiOperation(value = "查询列表信息")
    public Map<String, Object> selectList(Integer contentId, int page, int rows, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
//       Map<String,Object> map2=new HashMap<String,Object>();
//        map2.put("phone",request.getParameter("phone"));
        List<XContentFile> list = xContentFileService.selectList(contentId);
        PageInfo<XContentFile> pages = new PageInfo<XContentFile>(list);
        result.put("total", pages.getTotal());
        result.put("rows", pages.getList());
        return result;
    }


    @RequestMapping(value = "/delete")
    //@ApiOperation("删除")
    public Map<String, Object> delete(String[] ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        for (String id : ids) {
            xContentFileService.delete(Integer.parseInt(id));
        }
        map.put("isSuc", true);
        map.put("msg", "操作成功");
        return map;
    }

    @RequestMapping(value = "/updateByPrimaryKeySelective", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateByPrimaryKeySelective(
            HttpServletRequest request,
            XContentFile xContentFile
    ) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            MultipartFile multipartFile = null;
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                multipartFile = multipartRequest.getFile("uploadInfo");
            }
            String dir = Constants.XD_IMG_UPLOAD_DIR_TOMCAT;
            String sub = Constants.XD_IMG_UPLOAD_DIR_SUB;
            String server = Constants.XD_IMG_UPLOAD_SERVER;
            dir = dir + sub;
            FileUtil.mkdir(dir);
            //abc.jpg
            if (multipartFile!=null&& multipartFile.getOriginalFilename() != null && !"".equals(multipartFile.getOriginalFilename())){
               String originalFilename = multipartFile.getOriginalFilename();
                int index = originalFilename.lastIndexOf(".") + 1;
                String fileType = originalFilename.substring(index, originalFilename.length());
                String fileName = IdUtil.simpleUUID() + "." + fileType;
                String savePath = dir + fileName;
                multipartFile.transferTo(new File(savePath));
                xContentFile.setFilePath(savePath);
                xContentFile.setRelativePath(server + sub + fileName);
            } else {
               // xContentFile.setFilePath(null);
               // xContentFile.setRelativePath(null);
            }
            xContentFileService.updateByPrimaryKeySelective(xContentFile);
            map.put("isSuc", true);
            map.put("msg", "操作成功");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertSelective(HttpServletRequest request,
                                               XContentFile xContentFile
    ) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            MultipartFile multipartFile = null;
            if (request instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                multipartFile = multipartRequest.getFile("uploadInfo");
            }
            String dir = Constants.XD_IMG_UPLOAD_DIR_TOMCAT;
            String sub = Constants.XD_IMG_UPLOAD_DIR_SUB;
            String server = Constants.XD_IMG_UPLOAD_SERVER;
            dir = dir + sub;
            FileUtil.mkdir(dir);

            if (multipartFile!=null&& multipartFile.getOriginalFilename() != null && !"".equals(multipartFile.getOriginalFilename())) {
                //abc.jpg
                String originalFilename = multipartFile.getOriginalFilename();
                int index = originalFilename.lastIndexOf(".") + 1;
                String fileType = originalFilename.substring(index, originalFilename.length());
                String fileName = IdUtil.simpleUUID() + "." + fileType;
                String savePath = dir + fileName;
                multipartFile.transferTo(new File(savePath));
                xContentFile.setFilePath(savePath);
                xContentFile.setRelativePath(server + sub + fileName);
            } else {
                xContentFile.setFilePath(null);
                xContentFile.setRelativePath(null);
            }
            xContentFileService.insertSelective(xContentFile);
            map.put("isSuc", true);
            map.put("msg", "操作成功");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}